package com.nmes.naljava.server;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;


import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.Transform;
import com.google.appengine.api.images.ImagesService.OutputEncoding;

import com.nmes.naljava.PMF;
import com.nmes.naljava.entity.Account;

@SuppressWarnings("serial")
public class SaveDataServlet extends HttpServlet{
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        doPost(req, resp);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
   	
        PersistenceManager pm = PMF.get().getPersistenceManager();
        
        String action = new String();
        action = request.getParameter("action").toString();
        log( "request.getParameter('action'):" + action + "\n");
        
        if (request.getParameter("action").equals("newWrite")) {
        	response.sendRedirect("/board/newWrite.jsp");
            
        }else if (request.getParameter("action").equals("newWriteDo"))
        {
        	response.sendRedirect("/board/newWrite.jsp"); 

        } else if (request.getParameter("action").equals("boardList")) {
            // 전체 목록 처리
        	//System.out.printf("boardList1\n");
            Key k = KeyFactory.createKey(
            							Account.class.getSimpleName(),
            							new Integer(request.getParameter("id")
            							).intValue());

            log( "k.toString():" + k.toString()+ "\n" );
            Account a = pm.getObjectById(Account.class, k);
            log( "a.getTitle():" +a.getTitle()+ "\n");

             String query = "select from " + Account.class.getName();
                 //   + " where name == '" + request.getParameter("id")
                  //  + "'";
             log( "query:" + query+ "\n");
            
            List<Account> list = (List<Account>) pm.newQuery(query).execute();
            log( "list.size():" +list.size()+ "\n");
            request.setAttribute("list", list);
            System.out.printf("boardList\n");
           // response.sendRedirect("/board/accountDisplay.jsp");
           /// RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/board/accountDisplay.jsp");
           // dispatcher.forward(request, response);

        } else if (request.getParameter("action").equals("boardList2")) {
            // 전체 목록 처리
        	System.out.printf("boardList2\n");
            String query = "SELECT FROM com.nmes.naljava.entity.Account";

            List<Account> list = (List<Account>) pm.newQuery(query).execute();
            log( "list.size():" +list.size()+ "\n");
            request.setAttribute("list", list);  //객체 전달
            //response.sendRedirect("/board/accountDisplay.jsp");
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/board/accountDisplay.jsp");
            dispatcher.forward(request, response);
            System.out.printf("end boardList2\n");
        }//else if (action == "singleBoard" ) {
        else if (request.getParameter("action").equals("singleBoard")) {
            // 1건만 처리
        	System.out.printf("singBoard\n");
            String query = "select from com.nmes.naljava.entity.Account" 
                    + " where id == " + request.getParameter("id");
            log( "query:" + query+ "\n");
            
            List<Account> list = (List<Account>) pm.newQuery(query).execute();
            log( "list.size():" +list.size()+ "\n");
            request.setAttribute("list", list);
            
            // 객체 전달 이동
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/board/SingleDisplay.jsp");
            dispatcher.forward(request, response);
        }
        else if (request.getParameter("action").equals("updateBoardDisplay")) {
            // 1건만 처리
        	System.out.printf("updateBoardDisplay\n");
            String query = "select from com.nmes.naljava.entity.Account" 
                    + " where id == " + request.getParameter("id");
            log( "query:" + query+ "\n");
            
            List<Account> list = (List<Account>) pm.newQuery(query).execute();
            log( "list.size():" +list.size()+ "\n");
            request.setAttribute("list", list);
            
            // 객체 전달 이동
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/board/updateDisplay.jsp");
            dispatcher.forward(request, response);
        }
        
        
        //else if (action == "deleteBoard" ) {
        else if (request.getParameter("action").equals("deleteBoard")) {
            // 1건만 처리
        	System.out.printf("deleteBoard\n");
        	Key k = KeyFactory.createKey( Account.class.getSimpleName(), new Integer(request.getParameter("id")	).intValue());
        	log( "k.toString():" + k.toString()+ "\n" );
        	Account a = pm.getObjectById(Account.class,k);
            try {
            	pm.deletePersistent(a);
            } finally {
                pm.close();
            }
            
            response.sendRedirect("/SaveData?action=boardList2");  //단순 이동
            log( "end deleteBoard\n");
        }  
        log( "end SaveData\n");
        
        
    }
    
    
}